<template>
	<div class="addressList addressGl">
		<ul>
			<li class="addressList-item" v-for="(item,index) in addressList">
				<p><span>{{item.consignee}}</span><span>{{item.phone_mob}}</span></p>
				<p>{{item.region_name}}{{item.address}}</p>
				<ul class="addressGl-szbtn">
					<li :class="{'active':item.is_default==1}" @click="szmr(item)"><span><img src="../img-address/icon_dis@3x.png"/><img src="../img-address/icon_select @3x.png"/>设为默认地址</span></li>
					<li><router-link :to="{name:'nAddress',params:{id:item.addr_id}}"><img src="../img-address/icon_edit@3x.png"/>编辑</router-link></li>
					<li @click="deleteshow(index,item.addr_id)"><img src="../img-address/icon_del@3x.png"/>删除</li>
				</ul>
			</li>
		</ul>
		<div class="guanlidizhi"><router-link :to="{name:'nAddress',params:{id:0}}">新增收货地址</router-link></div>
		<div class="deleteBox" v-show="dltshow">
			<div class="addressdelete">
				<p>确定要<span>删除</span>该地址吗？</p>
				<span @click="deleteaddr()">确定</span><span @click="dltqx()">取消</span>
			</div>
			
		</div>
	</div>
</template>

<script>
	import addressGlcss from "../css/addressGl.css"
	
	var furl = 'http://www.ntyouxuan.com/'
	export default{
		name:"addressgl",
		data (){
			return{
				user:0,
				addressList:{},
				dltshow:false,
				dltitem:"",
				addrind:""
			}
		},
		created:function(){
			this.user=localStorage.getItem("nt_user");
		},
		methods:{
			szmr:function(item){
				var self=this;
				item.is_default=1;
				for(var i in this.addressList){
					if(this.addressList[i].addr_id===item.addr_id){
						$.ajax({
							type:"post",
							url:furl+"index.php?app=ajax&act=check_default",
							data:{"addr_id":item.addr_id,"token":self.user},
							dataType:"json",
							success:function(res){
								
							}
						});
					}else{
						this.addressList[i].is_default=0;
					}
				}
			},
			deleteshow:function(index,id){
				this.dltshow=true;
				this.addrind=index;
				this.dltitem=id;
			},
			deleteaddr:function(){
				var self=this;
				delete this.addressList[this.addrind];
				this.addressList=Object.assign({},this.addressList);	
				$.ajax({
					type:"get",
					url:furl+"index.php?app=ajax&act=drop_address",
					data:{"addr_id":self.dltitem,"token":self.user},
					dataType:"json",
					success:function(res){
						console.log(res);
					}
				});
				this.dltshow=false;
			},
			dltqx:function(){
				this.dltshow=false;
			}
		},
		mounted:function(){
			var self=this;
			console.log(self.user);
			$.ajax({
				type:"get",
				url:furl+'index.php?app=ajax&act=address',
				data:{'token':self.user},
				dataType:"json",
				success:function(res){
					console.log(res);
					self.addressList=res.retval;
				}
			})
		}
	}
</script>

<style>
</style>